package com.itheima.controller;

import com.itheima.domain.Book;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

// 2.1 TODO 设置Rest风格controller

// 2.2 TODO 设置全局访问路径前缀，注意使用复数形式，例如 user(s),account(s)

@RestController
@RequestMapping("/books")
public class BookController {

    // 2.3 TODO 根据CRUD类别不同，设置不同的请求方式，例如insert对应Post，update对应Put等
    // 2.4 TODO 注意参数的获取方式，按id查询使用路径参数/变量设置

    //json参数
    @PostMapping
    public String save(@RequestBody Book book) {
        System.out.println("book save ==> " + book);
        return "{'module':'book save success'}";
    }

    //路径参数
    @DeleteMapping("/{id}")
    public String deleteById(@PathVariable("id") Integer id) {
        System.out.println("book delete ==> " + id);
        return "{'module':'book delete success'}";
    }

    //json参数
    @PutMapping
    public String update(@RequestBody Book book) {
        System.out.println("book update ==> " + book);
        return "{'module':'book update success'}";
    }

    //路径参数
    @GetMapping("/{id}")
    public String getById(@PathVariable("id") Integer id) {
        System.out.println("book getById ==> " + id);
        return "{'module':'book getById success'}";
    }
	
	@GetMapping
    public List<Book> getAll() {
        System.out.println("book getAll is running ...");
        List<Book> bookList = new ArrayList<Book>();

        Book book1 = new Book();
        book1.setType("计算机");
        book1.setName("SpringMVC入门教程");
        book1.setDescription("小试牛刀");
        bookList.add(book1);

        Book book2 = new Book();
        book2.setType("计算机");
        book2.setName("SpringMVC实战教程");
        book2.setDescription("一代宗师");
        bookList.add(book2);

        Book book3 = new Book();
        book3.setType("计算机丛书");
        book3.setName("SpringMVC实战教程进阶");
        book3.setDescription("一代宗师呕心创作");
        bookList.add(book3);

        return bookList;
    }

}
